import 'package:flutter/material.dart';
import 'package:flutter_app/components/avatar_role_name.dart';
import 'package:flutter_app/components/comment_like_read.dart';
import 'package:flutter_app/config/app_colors.dart';
import 'package:flutter_app/model/article_model.dart';
import 'package:flutter_app/util/util.dart';

const double paddingSize = 20;
const double spaceSize = 10; //间距

class ArticleCard extends StatefulWidget {
  final ArticleItem articleItem;

  const ArticleCard({Key key, this.articleItem}) : super(key: key);

  @override
  _ArticleCardState createState() => _ArticleCardState();
}

class _ArticleCardState extends State<ArticleCard> {
  double boxSize;

  @override
  Widget build(BuildContext context) {
    boxSize = MediaQuery.of(context).size.width - paddingSize * 2;
    return Container(
      padding: EdgeInsets.all(paddingSize),
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _buildTitle(),
          SizedBox(height: paddingSize),
          _getCoverByType(),
          SizedBox(height: paddingSize),
          _bottom()
        ],
      ),
    );
  }

  ///基于图片的长度获取不同的随机类型
  int getTypeByLength() {
    int length = widget.articleItem.coverUrlList.length;
    int type;
    switch (length) {
      case 2:
        type = getRandomRangeInt(2, 3);
        break;
      case 3:
        type = getRandomRangeInt(2, 7);
        break;
      case 4:
        type = getRandomRangeInt(2, 8);
        break;
      default:
        type = 1;
    }
    return type;
  }

  _getCoverByType() {
    int coverType = getTypeByLength();
    Widget coverWidget;
    switch (coverType) {
      case 2:
        coverWidget = _cover2();
        break;
      case 3:
        coverWidget = _cover3();
        break;
      case 4:
        coverWidget = _cover4();
        break;
      default:
        coverWidget = _cover();
    }
    return coverWidget;
  }

  Widget _cover2() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: boxSize / 2 - spaceSize / 2,
          height: boxSize,
          child: _cover(index: 0),
        ),
        SizedBox(
          width: boxSize / 2 - spaceSize / 2,
          height: boxSize,
          child: _cover(index: 1),
        )
      ],
    );
  }

  Widget _cover3() {
    return Column(
      children: [
        SizedBox(
          width: boxSize,
          height: boxSize / 2 - spaceSize / 2,
          child: _cover(index: 0),
        ),
        SizedBox(height: spaceSize),
        SizedBox(
          width: boxSize,
          height: boxSize / 2 - spaceSize / 2,
          child: _cover(index: 1),
        )
      ],
    );
  }

  Widget _cover4() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        SizedBox(
          width: boxSize / 2 - spaceSize / 2,
          height: boxSize,
          child: _cover(index: 0),
        ),
        Column(
          children: [
            SizedBox(
              width: boxSize / 2 - spaceSize / 2,
              height: boxSize / 2 - spaceSize / 2,
              child: _cover(index: 1),
            ),
            SizedBox(height: spaceSize),
            SizedBox(
              width: boxSize / 2 - spaceSize / 2,
              height: boxSize / 2 - spaceSize / 2,
              child: _cover(index: 2),
            )
          ],
        ),
      ],
    );
  }

  Widget _buildTitle() {
    return Text(
      widget.articleItem.title,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(fontSize: 16, color: AppColors.active),
    );
  }

  Widget _cover({int index = 0}) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(10),
      child: FadeInImage.assetNetwork(
        placeholder: 'assets/images/common/lazy-1.png',
        image: widget.articleItem.coverUrlList[index],
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _bottom() {
    return Row(
      children: [
        Expanded(
          child: AvatarRoleName(
            avatar: widget.articleItem.user.coverPictureUrl,
            nickName: widget.articleItem.user.nickname,
            type: widget.articleItem.user.type,
          ),
        ),
        Expanded(
          child: CommentLikeRead(
            commentCount: widget.articleItem.commentCount,
            thumbUpCount: widget.articleItem.thumbUpCount,
            readCount: widget.articleItem.readCount,
          ),
        )
      ],
    );
  }
}
